package cn.jinhaze.service.impl;

import cn.jinhaze.common.pojo.dto.DishDTO;
import cn.jinhaze.common.pojo.dto.OrderDTO;
import cn.jinhaze.common.pojo.dto.OrderHandleDTO;
import cn.jinhaze.common.pojo.dto.OrderPageDTO;
import cn.jinhaze.common.pojo.entity.Dish;
import cn.jinhaze.common.pojo.entity.Orders;
import cn.jinhaze.common.pojo.entity.OrderDetail;
import cn.jinhaze.common.pojo.vo.DishVO;
import cn.jinhaze.common.pojo.vo.OrderVO;
import cn.jinhaze.mapper.OrderMapper;
import cn.jinhaze.service.DishService;
import cn.jinhaze.service.OrderDetailService;
import cn.jinhaze.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: L29
 * @create: 2024-12-25 12:23
 * @Version 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	@Autowired
	private DishService dishService;
	@Autowired
	private OrderDetailService orderDetailService;
	@Autowired
	private OrderMapper orderMapper;

	private static final String ORDER_GET = "order:get";

	@Override
	public OrderVO createOrder(OrderDTO orderDTO) {
		OrderVO orderVO = new OrderVO();
		Orders orders = new Orders();
		List<DishVO> dishVOList = new ArrayList<>();
		String orderNo = UUID.randomUUID().toString().replace("-", "");
		orders.setOrderNo(orderNo);
		copyProperties(orderDTO, orders);
		Long getId = redisTemplate.opsForValue().increment(ORDER_GET);
		orderVO.setGetId(getId);
		orders.setGetId(getId);
		orders.setStatus(1);
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		BigDecimal totalPrice = BigDecimal.ZERO;
		for (DishDTO dishDTO : orderDTO.getDishList()) {
			Dish dish = dishService.getById(dishDTO.getId());
			if (dish == null || dish.getStatus() == 0) {
				throw new RuntimeException("菜品不存在或已停售");
			}
			DishVO dishVO = new DishVO();
			BeanUtils.copyProperties(dish, dishVO);
			dishVOList.add(dishVO);
			BigDecimal price = dish.getPrice().multiply(new BigDecimal(dishDTO.getNumber()));
			totalPrice = totalPrice.add(price);
			// save order detail
			OrderDetail orderDetail = new OrderDetail();
			orderDetail.setDishId(dish.getId());
			orderDetail.setName(dish.getName() + " X " + dishDTO.getNumber());
			orderDetail.setImage(dish.getImage());
			orderDetail.setOrderId(orderNo);
			orderDetail.setDishId(dishDTO.getId());
			orderDetail.setTotalAmount(price);
			orderDetail.setAmount(dish.getPrice());
			orderDetailService.save(orderDetail);
		}
		orders.setAmount(totalPrice);
		orderVO.setTotalPrice(totalPrice);
		// save orders
		orderVO.setDishVOList(dishVOList);
		orderMapper.insert(orders);
		orderVO.setCreateTime(new Date());
		orderVO.setUpdateTime(new Date());
		orderVO.setOrderNo(orderNo);
		return orderVO;
	}

	@Override
	public List<OrderVO> list(OrderPageDTO orderPageDTO) {
		LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
		if (orderPageDTO.getOrderNo() != null && !orderPageDTO.getOrderNo().isEmpty()) {
			queryWrapper.eq(Orders::getOrderNo, orderPageDTO.getOrderNo());
		}
		if (orderPageDTO.getUserName() != null && !orderPageDTO.getUserName().isEmpty()) {
			queryWrapper.like(Orders::getUserName, orderPageDTO.getUserName());
		}
		if (orderPageDTO.getPhone() != null && !orderPageDTO.getPhone().isEmpty()) {
			queryWrapper.like(Orders::getPhone, orderPageDTO.getPhone());
		}
		if (orderPageDTO.getCreateTime() != null) {
			queryWrapper.ge(Orders::getCreateTime, orderPageDTO.getCreateTime());
		}
		if (orderPageDTO.getUpdateTime() != null) {
			queryWrapper.le(Orders::getUpdateTime, orderPageDTO.getUpdateTime());
		}
		if (orderPageDTO.getGetId() != null) {
			queryWrapper.eq(Orders::getGetId, orderPageDTO.getGetId());
		}
		List<Orders> orders = orderMapper.selectList(queryWrapper);
		List<OrderVO> orderVOList = new ArrayList<>();
		if (orders != null && !orders.isEmpty()) {
			for (Orders order : orders) {
				OrderVO orderVO = new OrderVO();
				BeanUtils.copyProperties(order, orderVO);
				orderVO.setCreateTime(order.getCreateTime());
				orderVO.setUpdateTime(order.getUpdateTime());
				orderVO.setDishVOList(orderDetailService.listByOrderId(order.getOrderNo()));
				orderVO.setTotalPrice(order.getAmount());
				orderVOList.add(orderVO);
			}
		}
		return orderVOList;
	}

	@Override
	public void handleOrder(OrderHandleDTO orderHandleDTO) {
		Orders orders = orderMapper.selectOne(new LambdaQueryWrapper<>(Orders.class).eq(Orders::getOrderNo, orderHandleDTO.getOrderNo()));
		if (orders == null) {
			throw new RuntimeException("订单不存在");
		}
		orders.setStatus(orderHandleDTO.getStatus());
		orders.setUpdateTime(new Date());
		LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(Orders::getOrderNo, orderHandleDTO.getOrderNo());
		orderMapper.update(lambdaQueryWrapper);
	}

	private void copyProperties(OrderDTO orderDTO, Orders orders) {
		orders.setUserName(orderDTO.getUserName());
		orders.setPhone(orderDTO.getPhone());
		orders.setRemark(orderDTO.getRemark());
	}
}